package com.monitor.app_monitor.service.quartz.task;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.monitor.app_monitor.entity.CellectInfo;
import com.monitor.app_monitor.entity.FileinfoData;
import com.monitor.app_monitor.entity.TaskLogAnnotation;
import com.monitor.app_monitor.service.quartz.utils.ExecuteScript;
import com.monitor.app_monitor.service.quartz.utils.FileinfoScriptExecResult;
import com.monitor.app_monitor.service.quartz.utils.ScriptExecResult;
import com.monitor.app_monitor.service.quartz.utils.logUtils;
import lombok.extern.slf4j.Slf4j;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

/**
 * @ClassName CellectDataCheck.java
 * @Author ning
 * @Description TODO
 * @CreateTime 2021年12月16日 10:18:00
 */

@DisallowConcurrentExecution //作业不并发
@Component
@Slf4j
public class CellectFileSizeCheck implements Job {

    @Resource
    @Qualifier("cellectfileSizeMap")
    private Map<String, AtomicInteger> cellectfileSizeMap;


    @Resource
    @Qualifier("cellectfileNbMap")
    private Map<String, AtomicInteger> cellectfileNbMap;


    @Resource
    private ExecuteScript executeScript;


    @Value("${dataPath}")
    private String dataPath;


    @Value("${fileinfo}")
    private  String fileInfoScript;

    @TaskLogAnnotation
    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        /**
         * 一干文件大小行数统计
         */
        final JobDataMap jobDataMap = jobExecutionContext.getJobDetail().getJobDataMap();
        List<CellectInfo> cellectInfos = (List<CellectInfo>) jobDataMap.get("cellectList");


        cellectInfos.forEach(cellectInfo -> {
            int resSizeCode = 0;
            int resNumberCode = 0;

            final FileinfoScriptExecResult fileinfoScriptExecResult;
            try {
                fileinfoScriptExecResult = executeScript.executeFileInfoScript(fileInfoScript,cellectInfo.getEmsName(),cellectInfo.getResType(),dataPath);
                String  SizeResult = fileinfoScriptExecResult.getFileSize();
                String  NumberResult = fileinfoScriptExecResult.getFileNb();

                resSizeCode = Integer.valueOf(SizeResult);
                resNumberCode = Integer.valueOf(NumberResult);




            } catch (Exception e) {

            }
            cellectfileSizeMap.get(cellectInfo.getEmsName()+cellectInfo.getResType()).set(resSizeCode);
            cellectfileNbMap.get(cellectInfo.getEmsName()+cellectInfo.getResType()).set(resNumberCode);

        });

        //String execlFile = createExeclFile(cellectInfos);

        //log.info("导出文件路径：{}",execlFile);

    }

    public String createExeclFile(List<CellectInfo> cellectInfos) {
        Date date = new Date();
        String strDateFormat = "yyyyMMddHHmm";
        SimpleDateFormat sdf = new SimpleDateFormat(strDateFormat);
        String DataFile = sdf.format(date);


        List<FileinfoData> fileSizeinfoDataList = new ArrayList<>();
        List<FileinfoData> fileNbinfoDataList = new ArrayList<>();

        List<String> emsList = new ArrayList<>();

        cellectInfos.forEach( cellectInfo -> {
            emsList.add(cellectInfo.getEmsName());
        });

        String[] res = {"ACTNIDME","ACTNIDPTP","CC","CTP","CV","EH","EQ","FD","FDTP","MD","ME","MER","MLSN","NRO","PG","PS","PTP","RA","RO","ROCC","ROTL","ROUT","SD","SH","SNC","SNCTP","SP","TL","TR"};

        List<String> collects = emsList.stream().distinct().collect(Collectors.toList());

        collects.forEach(collect -> {

            FileinfoData fileSizeinfoData = new FileinfoData();
            FileinfoData fileNbinfoData = new FileinfoData();


            HashMap<String, String> fileSize = new HashMap<>();
            HashMap<String, String> fileNb = new HashMap<>();


            for (int i = 0; i < res.length; i++) {
                int resSizeCode = 0;
                int resNumberCode = 0;
                String SizeString = "";

                final FileinfoScriptExecResult fileinfoScriptExecResult;
                try {
                    fileinfoScriptExecResult = executeScript.executeFileInfoScript(fileInfoScript,collect,res[i],dataPath);
                    String  SizeResult = fileinfoScriptExecResult.getFileSize();
                    String  NumberResult = fileinfoScriptExecResult.getFileNb();

                    resSizeCode = Integer.valueOf(SizeResult);
                    resNumberCode = Integer.valueOf(NumberResult);

                    if (resSizeCode>1024 ) {
                        SizeString = String.valueOf(resSizeCode/1024)+"M";
                    }else {
                        SizeString = String.valueOf(resSizeCode)+"KB";
                    }


                    fileSize.put(res[i],SizeString);
                    fileNb.put(res[i],String.valueOf(resNumberCode)+"行");

                } catch (Exception e) {

                }
            }

            fileSizeinfoData.setEmsName(collect);
            fileSizeinfoData.setACTNIDME(fileSize.get(res[0]));
            fileSizeinfoData.setACTNIDPTP(fileSize.get(res[1]));
            fileSizeinfoData.setCC(fileSize.get(res[2]));
            fileSizeinfoData.setCTP(fileSize.get(res[3]));
            fileSizeinfoData.setCV(fileSize.get(res[4]));
            fileSizeinfoData.setEH(fileSize.get(res[5]));
            fileSizeinfoData.setEQ(fileSize.get(res[6]));
            fileSizeinfoData.setFD(fileSize.get(res[7]));
            fileSizeinfoData.setFDTP(fileSize.get(res[8]));
            fileSizeinfoData.setMD(fileSize.get(res[9]));
            fileSizeinfoData.setME(fileSize.get(res[10]));
            fileSizeinfoData.setMER(fileSize.get(res[11]));
            fileSizeinfoData.setMLSN(fileSize.get(res[12]));
            fileSizeinfoData.setNRO(fileSize.get(res[13]));
            fileSizeinfoData.setPG(fileSize.get(res[14]));
            fileSizeinfoData.setPS(fileSize.get(res[15]));
            fileSizeinfoData.setPTP(fileSize.get(res[16]));
            fileSizeinfoData.setRA(fileSize.get(res[17]));
            fileSizeinfoData.setRO(fileSize.get(res[18]));
            fileSizeinfoData.setROCC(fileSize.get(res[19]));
            fileSizeinfoData.setROTL(fileSize.get(res[20]));
            fileSizeinfoData.setROUT(fileSize.get(res[21]));
            fileSizeinfoData.setSD(fileSize.get(res[22]));
            fileSizeinfoData.setSH(fileSize.get(res[23]));
            fileSizeinfoData.setSNC(fileSize.get(res[24]));
            fileSizeinfoData.setSNCTP(fileSize.get(res[25]));
            fileSizeinfoData.setSP(fileSize.get(res[26]));
            fileSizeinfoData.setTL(fileSize.get(res[27]));
            fileSizeinfoData.setTR(fileSize.get(res[28]));


            fileNbinfoData.setEmsName(collect);
            fileNbinfoData.setACTNIDME(fileNb.get(res[0]));
            fileNbinfoData.setACTNIDPTP(fileNb.get(res[1]));
            fileNbinfoData.setCC(fileNb.get(res[2]));
            fileNbinfoData.setCTP(fileNb.get(res[3]));
            fileNbinfoData.setCV(fileNb.get(res[4]));
            fileNbinfoData.setEH(fileNb.get(res[5]));
            fileNbinfoData.setEQ(fileNb.get(res[6]));
            fileNbinfoData.setFD(fileNb.get(res[7]));
            fileNbinfoData.setFDTP(fileNb.get(res[8]));
            fileNbinfoData.setMD(fileNb.get(res[9]));
            fileNbinfoData.setME(fileNb.get(res[10]));
            fileNbinfoData.setMER(fileNb.get(res[11]));
            fileNbinfoData.setMLSN(fileNb.get(res[12]));
            fileNbinfoData.setNRO(fileNb.get(res[13]));
            fileNbinfoData.setPG(fileNb.get(res[14]));
            fileNbinfoData.setPS(fileNb.get(res[15]));
            fileNbinfoData.setPTP(fileNb.get(res[16]));
            fileNbinfoData.setRA(fileNb.get(res[17]));
            fileNbinfoData.setRO(fileNb.get(res[18]));
            fileNbinfoData.setROCC(fileNb.get(res[19]));
            fileNbinfoData.setROTL(fileNb.get(res[20]));
            fileNbinfoData.setROUT(fileNb.get(res[21]));
            fileNbinfoData.setSD(fileNb.get(res[22]));
            fileNbinfoData.setSH(fileNb.get(res[23]));
            fileNbinfoData.setSNC(fileNb.get(res[24]));
            fileNbinfoData.setSNCTP(fileNb.get(res[25]));
            fileNbinfoData.setSP(fileNb.get(res[26]));
            fileNbinfoData.setTL(fileNb.get(res[27]));
            fileNbinfoData.setTR(fileNb.get(res[28]));



            fileSizeinfoDataList.add(fileSizeinfoData);
            fileNbinfoDataList.add(fileNbinfoData);

        });


        ExcelWriter excelWriter = EasyExcel.write("/home/deployer/openresty/nginx/html/cellect-download/FileSize"+DataFile+".xlsx").build();

        WriteSheet writeSheet = EasyExcel.writerSheet(0, "文件大小").head(FileinfoData.class).build();
        excelWriter.write(fileSizeinfoDataList, writeSheet);


        writeSheet = EasyExcel.writerSheet(1, "文件行数").head(FileinfoData.class).build();
        excelWriter.write(fileNbinfoDataList, writeSheet);
        //千万别忘记finish 会帮忙关闭流
        excelWriter.finish();
        return "/home/deployer/openresty/nginx/html/cellect-download/FileSize"+DataFile+".xlsx";
    }
}
